package com.atguigu.gmalllogger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Archie
 * @date 2021-10-16 19:22
 * @description
 */
@RestController
@Slf4j
public class LoggerController {

	@Autowired
	private KafkaTemplate kafkaTemplate;

	@RequestMapping("/applog")
	public String logger(@RequestParam("param") String log) {

		// 1. 数据落盘
		saveToDisk(log);

		// 2. 数据写入Kafka中
		sendToKafka(log);

		return "ok";
	}

	private void saveToDisk(String stringLog) {
		log.info(stringLog);
	}

	private void sendToKafka(String stringLog) {
		kafkaTemplate.send("ods_log", stringLog);
	}

}
